package cn.b.sky.message.service.impl;

import cn.b.sky.message.service.MessageSenderService;
import cn.b.sky.message.util.MailSenderInfo;
import cn.b.sky.message.util.SimpleMailSender;
import cn.b.sky.user.dao.UserDao;
import cn.b.sky.user.service.model.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class EmailMessageSenderImpl implements MessageSenderService {

  private String smtpServer;
  private String smtpServerPort;
  private String smtpUserName;
  private String smtpPassword;
  private String smtpFromAddress;
  
  @Resource
  UserDao userDao;

  /**
   * 发送邮件消息
   * 
   * @param message
   * @param userCode
   */
  @Override
  public void sentMessage(String messageCode, String message, String siteCode, String userCode) {
    UserInfo user = userDao.selectUser(siteCode, userCode);
    if (!StringUtils.isEmpty(user.getEmail())) {
      MailSenderInfo mailInfo = new MailSenderInfo();
      mailInfo.setMailServerHost(smtpServer);
      mailInfo.setMailServerPort(smtpServerPort);
      mailInfo.setValidate(true);
      mailInfo.setUserName(smtpUserName);
      mailInfo.setPassword(smtpPassword);// 您的邮箱密码
      mailInfo.setFromAddress(smtpFromAddress);
      mailInfo.setToAddress(user.getEmail());
      mailInfo.setSubject("IME自动邮件消息 ");
      mailInfo.setContent(message + "\r\n\r\n\r\n" + "此消息由IME系统自动发出，请勿回复！");
      // 这个类主要来发送邮件
      SimpleMailSender sms = new SimpleMailSender();

      sms.sendTextMail(mailInfo);// 发送html格式
    }

  }

}
